<!DOCTYPE html>
<html>
  <head lang="en">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Basic JSON Editor Example</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-uWxY/CJNBR+1zjPWmfnSnVxwRheevXITnMqoEIeG1LJrdI0GlVs/9cVSyPYXdcSF" crossorigin="anonymous">
    <link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-icons.min.css">
    <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
  </head>
  <body>

  {{{ html.navbar(out,"/web/main") }}}

  <div class="container overflow-hidden">

    <div class="row">
      <div class="col-md-auto">
        <button type="button" id="SaveS" class="btn btn-outline-primary">Save</button>
      </div>
      <div class="col-md-auto">
        <button type="button" id="ReadS" class="btn btn-outline-primary">Read</button>
      </div>
      <div class="col-md-auto">
        <input type="text" id="FileName" value="" class="form-control" placeholder="File name">
      </div>
    </div>
    <div id='editor_holder'></div>

    <script>
      // Initialize the editor with a JSON schema
      var editor = new JSONEditor(document.getElementById('editor_holder'),{
        theme: 'bootstrap4',
        iconlib: 'spectre',
        disable_edit_json: true,
        disable_properties: true,
        ajax: true,
        schema: { "$ref": "testdata.json", format: "grid" }
      });
      // Hook up the save button to save to the console
      document.getElementById('SaveS').addEventListener('click',function() {
        // Get the value from the editor
       fetch("/TQAE/saveSettings", {
           method: "POST", 
           body: JSON.stringify({"name":document.getElementById("FileName").value, "content":editor.getValue()})
        }).then(res => {
          console.log("Request complete! response:", res);
        });
      });
      document.getElementById('ReadS').addEventListener('click',function() {
        // Get the value from the editor
       fetch("/TQAE/readSettings", {
           method: "POST", 
           body: JSON.stringify({"name":document.getElementById("FileName").value})
        }).then(res => {
          console.log("Request complete! response:", res);
        });
      });
    </script>
  </div>
  {{{ html.footer(out) }}}
  </body>
</html>
